import hashlib
from apps.work.models import All_Kq_Infos, All_Kq_Orders, Kq_Extend_infos, Kq_Maintain_infos, Kq_Price_infos, \
    Kq_All_Pumps, Kq_All_Projects, Comp_Dept,Comp_Role,Comp_User
from apps.vadmin.op_drf.serializers import CustomModelSerializer
from apps.vadmin.permission.serializers import PostSerializer
from apps.vadmin.permission.serializers import RoleSerializer
from apps.vadmin.op_drf.validator import CustomUniqueValidator
from rest_framework import serializers
from django.contrib.auth import get_user_model
UserProfile = get_user_model()
from apps.vadmin.system.models import MessagePush
# ================================================= #
# ************** 用户管理 序列化器  ************** #
# ================================================= #
class UserProfileSerializer(CustomModelSerializer):
    """
    简单用户序列化器
    """
    deptId = serializers.IntegerField(source='dept.id', read_only=True)


    class Meta:
        model = Comp_User
        depth = 1
        exclude = ('description', 'creator', 'modifier')




class UserProfileCreateUpdateSerializer(CustomModelSerializer):
    """
    用户管理 创建/更新时的列化器
    """

    def save(self, **kwargs):
        self.validated_data['dept_id'] = self.initial_data.get('deptId', None)
        data = super().save(**kwargs)
        return data



    class Meta:
        model = Comp_User
        exclude = ('description', 'creator', 'modifier')
        read_only_fields = ('dept',)




# ================================================= #
# ************** 角色管理 序列化器  ************** #
# ================================================= #
class RoleSerializer(CustomModelSerializer):
    """
    角色管理 简单序列化器
    """

    class Meta:
        model = Comp_Role
        exclude = ('description', 'creator', 'modifier')


class ExportRoleSerializer(CustomModelSerializer):
    """
    导出 角色管理 简单序列化器
    """
    # dataScope = serializers.SerializerMethodField()

    # def get_dataScope(self, obj):
    #     dataScope = obj.get_dataScope_display()
    #     return dataScope

    class Meta:
        model = Comp_Role
        fields = ('id', 'roleName', 'roleSort', 'status', 'creator', 'modifier', 'remark')


class RoleSimpleSerializer(CustomModelSerializer):
    """
    角色管理 极简单序列化器
    """

    class Meta:
        model = Comp_Role
        fields = ('id', 'roleName', 'roleKey', 'status')


class RoleCreateUpdateSerializer(CustomModelSerializer):
    """
    角色管理 创建/更新时的列化器
    """
    # menu = MenuSerializer(many=True, read_only=True)
    # dept = DeptSerializer(many=True, read_only=True)

    def validate(self, attrs: dict):
        return super().validate(attrs)

    def save(self, **kwargs):
        data = super().save(**kwargs)
        # data.dept.set(self.initial_data.get('dept'))
        # data.menu.set(self.initial_data.get('menu'))
        return data

    class Meta:
        model = Comp_Role
        fields = '__all__'


# ================================================= #
# ************** 部门管理 序列化器  ************** #
# ================================================= #

class WorkDeptSerializer(CustomModelSerializer):
    """
    部门管理 简单序列化器
    """
    parentId = serializers.IntegerField(source="parentId.id", default=0)

    class Meta:
        model = Comp_Dept
        exclude = ('description', 'creator', 'modifier')


class WorkDeptCreateUpdateSerializer(CustomModelSerializer):
    """
    部门管理 创建/更新时的列化器
    """

    def validate(self, attrs: dict):
        return super().validate(attrs)

    class Meta:
        model = Comp_Dept
        fields = '__all__'


class WorkDeptTreeSerializer(serializers.ModelSerializer):
    """
    部门树形架构序列化器:递归序列化所有深度的子部门
    """
    label = serializers.CharField(source='deptName', default='')
    parentId = serializers.IntegerField(source="parentId.id", default=0)

    class Meta:
        model = Comp_Dept
        fields = ('id', 'label', 'parentId', 'status')


# ================================================= #
# ************** 客户库 序列化器  ************** #
# ================================================= #
class WorkSerializer(CustomModelSerializer):
    """
    所有工作 简单序列化器
    """

    class Meta:
        model = All_Kq_Infos
        fields = '__all__'


class WorkCreateUpdateSerializer(CustomModelSerializer):
    """
    我的工作 创建/更新时的列化器
    """

    # 此处可写定制的 创建/更新 内容
    def validate(self, attrs: dict):
        return super().validate(attrs)

    class Meta:
        model = All_Kq_Infos
        fields = '__all__'


class ExportWorkSerializer(CustomModelSerializer):
    """
    导出 凯泉客户库 简单序列化器
    """

    class Meta:
        model = All_Kq_Infos
        fields = ('id', 'compName', 'compOwner', 'regCap', 'realCap', 'foundDay', 'opStatus', 'province', 'city',
                  'district', 'street', 'compType', 'industry', 'oldName', 'empNum', 'phone', 'otherPhone', 'address',
                  'url', 'email', 'businessScope')


# ================================================= #
# ************** 订单中心 序列化器  ************** #
# ================================================= #
class OrderSerializer(CustomModelSerializer):
    """
    简单订单序列化器
    """

    class Meta:
        model = All_Kq_Orders
        fields = '__all__'


class OrderCreateUpdateSerializer(CustomModelSerializer):
    """
    订单 创建/更新时的列化器
    """

    def validate(self, attrs: dict):
        return super().validate(attrs)

    class Meta:
        model = All_Kq_Orders
        fields = '__all__'


class ExportOrderSerializer(CustomModelSerializer):
    """
    导出 我的工作 简单序列化器
    """

    class Meta:
        model = All_Kq_Orders
        fields = ('id', 'orderId', 'compName', 'projectName', 'products', 'dealNum', 'orderDate',
                  'orderType', 'collaborator', 'isEmployed', 'sex', 'position', 'maxDepartment',
                  'department')


# ================================================= #
# ************** Kq拓展信息 序列化器  ************** #
# ================================================= #
class KqExtendInfoSerializer(CustomModelSerializer):
    """
    简单订单序列化器
    """

    class Meta:
        model = Kq_Extend_infos
        fields = '__all__'


class KqExtendInfoCreateUpdateSerializer(CustomModelSerializer):
    """
    订单 创建/更新时的列化器
    """

    def validate(self, attrs: dict):
        return super().validate(attrs)

    class Meta:
        model = Kq_Extend_infos
        fields = '__all__'


# ================================================= #
# ************** 维护 序列化器  ************** #
# ================================================= #
class KqMaintainSerializer(CustomModelSerializer):
    """
    简单订单序列化器
    """

    class Meta:
        model = Kq_Maintain_infos
        fields = '__all__'


class KqMaintainCreateUpdateSerializer(CustomModelSerializer):
    """
    订单 创建/更新时的列化器
    """

    def validate(self, attrs: dict):
        return super().validate(attrs)

    class Meta:
        model = Kq_Maintain_infos
        fields = '__all__'


class ExportMaintainSerializer(CustomModelSerializer):
    """
    导出 我的工作 简单序列化器
    """

    class Meta:
        model = Kq_Maintain_infos
        fields = ('create_datetime', 'compName', 'name', 'position', 'phone', 'extendDesc')


# ================================================= #
# ************** 报价 序列化器  ************** #
# ================================================= #
class KqPriceSerializer(CustomModelSerializer):
    """
    简单订单序列化器
    """

    class Meta:
        model = Kq_Price_infos
        fields = '__all__'


class KqPriceCreateUpdateSerializer(CustomModelSerializer):
    """
    订单 创建/更新时的列化器
    """

    def validate(self, attrs: dict):
        return super().validate(attrs)

    class Meta:
        model = Kq_Price_infos
        fields = '__all__'


# ================================================= #
# ************** pumps 序列化器  ************** #
# ================================================= #
class KqPumpsSerializer(CustomModelSerializer):
    """
    简单订单序列化器
    """

    class Meta:
        model = Kq_All_Pumps
        fields = '__all__'


class KqPumpsCreateUpdateSerializer(CustomModelSerializer):
    """
    订单 创建/更新时的列化器
    """

    def validate(self, attrs: dict):
        return super().validate(attrs)

    class Meta:
        model = Kq_All_Pumps
        fields = '__all__'


class ExportKqPumpsSerializer(CustomModelSerializer):
    """
    导出 我的工作 简单序列化器
    """

    class Meta:
        model = Kq_All_Pumps
        fields = ('id', 'kind', 'type', 'flow', 'height', 'power', 'rev',
                  'calibre', 'compType', 'description')


# ================================================= #
# ************** Kq项目 序列化器  ************** #
# ================================================= #
class KqAllProjectsSerializer(CustomModelSerializer):
    """
    简单订单序列化器
    """

    class Meta:
        model = Kq_All_Projects
        fields = '__all__'


class KqAllProjectCreateUpdateSerializer(CustomModelSerializer):
    """
    订单 创建/更新时的列化器
    """

    def validate(self, attrs: dict):
        return super().validate(attrs)

    class Meta:
        model = Kq_All_Projects
        fields = '__all__'
